/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 *
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.core;

import static org.springframework.util.ReflectionUtils.*;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.openinsula.vulcano.core.annotation.RequiresDependencyInjection;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.AnnotationBeanWiringInfoResolver;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.wiring.BeanConfigurerSupport;
import org.springframework.beans.factory.wiring.ClassNameBeanWiringInfoResolver;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils.FieldCallback;
import org.springframework.util.ReflectionUtils.FieldFilter;

public class BeanConfigurerSupportUtils {

	private static final ClassNameBeanWiringInfoResolver classNameBeanWiringInfoResolver = new ClassNameBeanWiringInfoResolver();

	private static final AnnotationBeanWiringInfoResolver annotationBeanWiringInfoResolver = new AnnotationBeanWiringInfoResolver();

	public static void configureBean(final BeanFactory beanFactory, final Object bean) {
		Assert.notNull(bean, "beanFactory must not be null");
		Assert.notNull(bean, "bean must not be null");

		LinkedList<Object> beanList = new LinkedList<Object>();
		resolveBeans(beanList, bean);

		for (Object innerBean : beanList) {
			BeanConfigurerSupport beanConfigurerSupport = new BeanConfigurerSupport();
			Configurable configurable = innerBean.getClass().getAnnotation(Configurable.class);

			if (configurable == null) {
				beanConfigurerSupport.setBeanWiringInfoResolver(classNameBeanWiringInfoResolver);
			} else {
				beanConfigurerSupport.setBeanWiringInfoResolver(annotationBeanWiringInfoResolver);
			}

			beanConfigurerSupport.setBeanFactory(beanFactory);
			beanConfigurerSupport.configureBean(innerBean);
		}

	}

	private static void resolveBeans(final List<Object> beanList, final Object rootBean) {
		beanList.add(rootBean);

		doWithFields(rootBean.getClass(), new FieldCallback() {

			@Override
			public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException {
				makeAccessible(field);

				Object innerBean = field.get(rootBean);

				if (innerBean != null) {

					if (field.getType().isArray()) {
						for (int i = 0, j = Array.getLength(innerBean); i < j; i++) {
							resolveBeans(beanList, Array.get(innerBean, i));
						}

					} else if (innerBean instanceof Iterator) {
						resolveIteration((Iterator<?>) innerBean);

					} else if (innerBean instanceof Iterable) {
						resolveIteration(((Iterable<?>) innerBean).iterator());

					} else {
						resolveBeans(beanList, innerBean);
					}
				}
			}

			private void resolveIteration(final Iterator<?> iterator) {
				while (iterator.hasNext()) {
					Object obj = iterator.next();

					if (obj != null) {
						resolveBeans(beanList, obj);
					}
				}
			}

		}, new FieldFilter() {

			@Override
			public boolean matches(final Field field) {
				return !field.isEnumConstant() && field.isAnnotationPresent(RequiresDependencyInjection.class);
			}

		});

	}
}
